package com.demo.controller;


import com.demo.entity.People;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/elasticSearch")
public class ElasticSearchController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    /**
     * 创建索引并设置映射
     */
    @PostMapping("/buildIndexAndMapping")
    public void buildIndexAndMapping() {
        IndexOperations indexOps = restTemplate.indexOps(People.class);
        // 创建索引
        boolean flag = indexOps.create();
        if (flag) {
            log.info("创建索引成功");
            try {
                indexOps.putMapping(// 设置映射
                        //根据创建indexOps时的类对象，创建映射信息
                        indexOps.createMapping()
                );
                log.info("设置映射【{}】成功", indexOps.createMapping());
            } catch (Exception e) {
                log.error("设置映射失败");
            }
        } else {
            log.error("索引创建失败");
        }
    }

    /**
     * 删除索引
     */
    @PostMapping("/deleteIndex")
    public void deleteIndex() {
        try {
            restTemplate.indexOps(People.class).delete();
            log.info("删除索引成功");
        } catch (Exception e) {
            log.error("删除索引失败");
        }
    }

    /**
     * 添加节点信息
     */
    @PostMapping("/saveDocument")
    public void saveDocument() {
        try {
            // 单个新增 主键为空会自动生成
            restTemplate.save(new People(null, "张三", "12"));
            // 批量新增
//            List<People> peopleList=new ArrayList<>();
//            peopleList.add(new People(null,"zs1","11"));
//            peopleList.add(new People(null,"zs2","12"));
//            peopleList.add(new People(null,"zs3","13"));
//            restTemplate.save(peopleList);
            log.info("添加节点信息成功");
        } catch (Exception e) {
            log.error("添加信息失败", e);
        }
    }

    /**
     * 查询指定索引节点信息
     *
     * @return
     */
    @PostMapping("/queryDocument")
    public List<People> queryDocument() {
        // 查询指定索引下所有节点信息
        List<People> peopleList = new ArrayList<>();
        try {
            SearchHits<People> peopleSearchHits = restTemplate.search(Query.findAll(), People.class);
            Iterator<SearchHit<People>> iterator = peopleSearchHits.iterator();
            while (iterator.hasNext()) {
                SearchHit<People> searchHit = iterator.next();
                peopleList.add(searchHit.getContent());
            }
            return peopleList;
        } catch (Exception e) {
            log.error("查询失败");
        }
        return null;
        // 查询指定id节点
//        People people = restTemplate.get("Y783H34BtYGMfyQHwvbg", People.class);
//        peopleList.add(people);
//        return peopleList;
    }
}
